<template>
  <div ref="refNodeWrap">
    <div v-if="m_nodeConfig.type!=4" class="node-wrap">
      <div class="node-wrap-box" :class="(m_nodeConfig.type==0?'start-node ':'')+(isTried&&m_nodeConfig.error?'active error':'')">
        <div>
          <div class="title" :style="'background: rgb('+ ['87, 106, 149','255, 148, 62','50, 150, 250'][m_nodeConfig.type] +');'">
            <el-icon v-show="m_nodeConfig.type==0" class="m-icon"><i-ep-user-filled /></el-icon>
            <el-icon v-show="m_nodeConfig.type==1" class="m-icon"><i-ep-user-filled /></el-icon>
            <el-icon v-show="m_nodeConfig.type==2" class="m-icon"><i-ep-promotion /></el-icon>
            <span>{{ m_nodeConfig.nodeName }}</span>
            <el-icon v-if="m_nodeConfig.type!=0" class="close" @click="delNode()"><i-ep-close /></el-icon>
          </div>
          <div class="content" @click="setPerson">
            <div v-if="m_nodeConfig.type==0" class="text">{{ func.arrToStr(flowPermission)?func.arrToStr(flowPermission):'所有人' }}</div>
            <div v-if="m_nodeConfig.type==1" class="text">
              <span v-if="!func.setApproverStr(m_nodeConfig)" class="placeholder">请选择审核人</span>
              {{ func.setApproverStr(m_nodeConfig) }}
            </div>
            <div v-if="m_nodeConfig.type==2" class="text">
              <span v-if="!func.copyerStr(m_nodeConfig)" class="placeholder">请选择抄送人</span>
              {{ func.copyerStr(m_nodeConfig) }}
            </div>
            <el-icon class="m-icon arrow"><i-ep-arrow-right /></el-icon>
          </div>
          <div v-if="isTried&&m_nodeConfig.error" class="error_tip">
            <el-icon style="color: rgb(242, 86, 67);"><i-ep-warning-filled /></el-icon>
          </div>
        </div>
      </div>
      <add-node v-model:child-node-p="m_nodeConfig.childNode" />
    </div>
    <div v-if="m_nodeConfig.type==4" class="branch-wrap">
      <div class="branch-box-wrap">
        <div class="branch-box">
          <button class="add-branch" @click="addTerm">添加条件</button>
          <div v-for="(item,index) in m_nodeConfig.conditionNodes" :key="index" class="col-box">
            <div class="condition-node">
              <div class="condition-node-box">
                <div class="auto-judge" :class="isTried&&item.error?'error active':''" @click.self="setPerson(item.priorityLevel)">
                  <div v-if="index!=0" class="sort-left" @click.stop="arrTransfer(index,-1)">&lt;</div>
                  <div class="title-wrapper">
                    <span class="editable-title">{{ item.nodeName }}</span>
                    <span class="priority-title" @click="setPerson(item.priorityLevel)">优先级{{ item.priorityLevel }}</span>
                    <el-icon class="close" @click="delTerm(index)"><i-ep-close /></el-icon>
                  </div>
                  <div
                    v-if="index!=m_nodeConfig.conditionNodes.length-1"
                    class="sort-right"
                    @click.stop="arrTransfer(index)"
                  >
                    &gt;
                  </div>
                  <div class="content" @click="setPerson(item.priorityLevel)">
                    <template v-for="res in conditionMsg.msgs" :key="res.index">
                      <template v-if="res.index===index">
                        <div v-for="msg in res.value" :key="msg" class="flex">
                          <span>[</span>
                          <span class="label"> {{ msg }} </span>
                          <span>]</span>
                        </div>
                      </template>
                    </template>
                  </div>
                  <div v-if="isTried&&item.error" class="error_tip">
                    <el-icon style="color: rgb(242, 86, 67);"><i-ep-warning-filled /></el-icon>
                  </div>
                </div>
                <add-node v-model:child-node-p="item.childNode" />
              </div>
            </div>
            <node-wrap v-if="item.childNode" v-model:node-config="item.childNode" />
            <div v-if="index==0" class="top-left-cover-line" />
            <div v-if="index==0" class="bottom-left-cover-line" />
            <div v-if="index==m_nodeConfig.conditionNodes.length-1" class="top-right-cover-line" />
            <div v-if="index==m_nodeConfig.conditionNodes.length-1" class="bottom-right-cover-line" />
          </div>
        </div>
        <add-node v-model:child-node-p="m_nodeConfig.childNode" />
      </div>
    </div>
    <node-wrap v-if="m_nodeConfig.childNode" v-model:node-config="m_nodeConfig.childNode" />
  </div>
</template>

<script>
import { ref, computed, watch, getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import func from '../../utils/preload.js'

export default {
  name: 'NodeWrap',
  props: {
    /** 节点配置 */
    nodeConfig: {
      type: Object,
      default: () => {}
    },
    /** 发起人, 不选默认所有人 */
    flowPermission: {
      type: Array,
      default: () => []
    }
  },
  emits: ['update:nodeConfig', 'update:flowPermission'],
  setup(props, { emit }) {
    const store = useStore()
    const refNodeWrap = ref(null)
    const cit = getCurrentInstance().proxy
    const { uid: _uid } = cit.$

    const isTried = computed(() => store.state.app.workflow.isTried)
    const flowPermission1 = computed(() => store.state.app.workflow.flowPermission1)
    const approverConfig1 = computed(() => store.state.app.workflow.approverConfig1)
    const copyerConfig1 = computed(() => store.state.app.workflow.copyerConfig1)
    const conditionsConfig1 = computed(() => store.state.app.workflow.conditionsConfig1)

    const m_nodeConfig = computed(() => props.nodeConfig)

    if (m_nodeConfig.value.type === 1) {
      m_nodeConfig.value.error = !func.setApproverStr(m_nodeConfig.value)
    } else if (m_nodeConfig.value.type === 2) {
      m_nodeConfig.value.error = !func.copyerStr(m_nodeConfig.value)
    } else if (m_nodeConfig.value.type === 4) {
      for (var i = 0; i < m_nodeConfig.value.conditionNodes.length; i++) {
        m_nodeConfig.value.conditionNodes[i].error = func.conditionStr(m_nodeConfig.value, i) === '请设置条件' && i !== m_nodeConfig.value.conditionNodes.length - 1
      }
    }

    watch(flowPermission1, (data) => {
      if (data.flag && data.id === _uid) {
        console.log('flowPermission1 :>> ', data)
        emit('update:flowPermission', data.value)
      }
    })
    watch(approverConfig1, (data) => {
      if (data.flag && data.id === _uid) {
        console.log('approverConfig1 :>> ', data)
        emit('update:nodeConfig', data.value)
      }
    })
    watch(copyerConfig1, (data) => {
      if (data.flag && data.id === _uid) {
        console.log('copyerConfig1 :>> ', data)
        emit('update:nodeConfig', data.value)
      }
    })
    watch(conditionsConfig1, (data) => {
      if (data.flag && data.id === _uid) {
        emit('update:nodeConfig', data.value)
      }
    })

    /** 删除节点 */
    const delNode = () => {
      emit('update:nodeConfig', m_nodeConfig.value.childNode)
    }

    /** 添加条件 */
    const addTerm = () => {
      const len = m_nodeConfig.value.conditionNodes.length + 1
      m_nodeConfig.value.conditionNodes.push({
        'nodeName': '条件' + len,
        'type': 3,
        'priorityLevel': len,
        'conditionList': [],
        'nodeUserList': [],
        'childNode': null
      })
      for (var i = 0; i < m_nodeConfig.value.conditionNodes.length; i++) {
        m_nodeConfig.value.conditionNodes[i].error = func.conditionStr(m_nodeConfig.value, i) === '请设置条件' && i !== m_nodeConfig.value.conditionNodes.length - 1
      }
      emit('update:nodeConfig', m_nodeConfig.value)
    }

    /** 删除条件 */
    const delTerm = (index) => {
      m_nodeConfig.value.conditionNodes.splice(index, 1)
      m_nodeConfig.value.conditionNodes.map((item, index) => {
        item.priorityLevel = index + 1
        item.nodeName = `条件${index + 1}`
      })
      for (var i = 0; i < m_nodeConfig.value.conditionNodes.length; i++) {
        m_nodeConfig.value.conditionNodes[i].error = func.conditionStr(m_nodeConfig.value, i) === '请设置条件' && i !== m_nodeConfig.value.conditionNodes.length - 1
      }
      emit('update:nodeConfig', m_nodeConfig.value)
      if (m_nodeConfig.value.conditionNodes.length === 1) {
        if (m_nodeConfig.value.childNode) {
          if (m_nodeConfig.value.conditionNodes[0].childNode) {
            reData(m_nodeConfig.value.conditionNodes[0].childNode, m_nodeConfig.value.childNode)
          } else {
            m_nodeConfig.value.conditionNodes[0].childNode = m_nodeConfig.value.childNode
          }
        }
        emit('update:nodeConfig', m_nodeConfig.value.conditionNodes[0].childNode)
      }
    }

    /** 递归子节点 */
    const reData = (data, addData) => {
      if (!data.childNode) {
        data.childNode = addData
      } else {
        reData(data.childNode, addData)
      }
    }

    /** 点击节点 */
    const setPerson = (priorityLevel) => {
      var { type } = m_nodeConfig.value
      if (type === 0) {
        store.dispatch('app/workflow/setPromoter', true)
        store.dispatch('app/workflow/setFlowPermission', {
          value: props.flowPermission,
          flag: false,
          id: _uid
        })
      } else if (type === 1) {
        store.dispatch('app/workflow/setApprover', true)
        store.dispatch('app/workflow/setApproverConfig', {
          value: { ...JSON.parse(JSON.stringify(m_nodeConfig.value)),
            ...{ settype: m_nodeConfig.value.settype ? m_nodeConfig.value.settype : 1 }},
          flag: false,
          id: _uid
        })
      } else if (type === 2) {
        store.dispatch('app/workflow/setCopyer', true)
        store.dispatch('app/workflow/setCopyerConfig', {
          value: JSON.parse(JSON.stringify(m_nodeConfig.value)),
          flag: false,
          id: _uid
        })
      } else {
        store.dispatch('app/workflow/setCondition', true)
        store.dispatch('app/workflow/setConditionsConfig', {
          value: JSON.parse(JSON.stringify(m_nodeConfig.value)),
          priorityLevel,
          flag: false,
          id: _uid
        })
      }
    }

    /** 条件左右移动 */
    const arrTransfer = (index, type = 1) => { // 向左-1,向右1
      m_nodeConfig.value.conditionNodes[index] = m_nodeConfig.value.conditionNodes.splice(index + type, 1, m_nodeConfig.value.conditionNodes[index])[0]
      m_nodeConfig.value.conditionNodes.map((item, index) => {
        item.priorityLevel = index + 1
      })
      for (var i = 0; i < m_nodeConfig.value.conditionNodes.length; i++) {
        m_nodeConfig.value.conditionNodes[i].error = func.conditionStr(m_nodeConfig.value, i) === '请设置条件' && i !== m_nodeConfig.value.conditionNodes.length - 1
      }
      emit('update:nodeConfig', m_nodeConfig.value)
    }

    /** 条件内容展示 */
    const conditionMsg = computed(() => {
      const hasCondition = m_nodeConfig.value.conditionNodes.some(e => {
        return e.nodeUserList.length > 0 || e.conditionList.length > 0
      })

      const result = {
        error: hasCondition,
        msgs: m_nodeConfig.value.conditionNodes.map((m, index) => {
          const msg = {
            index,
            value: []
          }
          if (m.nodeUserList.length === 0 && m.conditionList.length === 0) {
            msg.value.push(hasCondition ? '其他条件进入此流程' : '请设置条件')
          } else {
            if (m.nodeUserList.length > 0) {
              msg.value.push('发起人：' + m.nodeUserList.map(n => n.name).join(','))
            }
            m.conditionList.forEach(e => {
              var { type, label, value, optType, optionItems } = e
              if (label && value && (type === 'input' || type === 'textarea')) {
                msg.value.push(label + '：%' + value + '%')
              }
              if (label && type === 'number' && optType) {
                msg.value.push(label + ' ' + optType + ' ' + value)
              }
              if (label && value && type === 'radio' && optionItems) {
                const v = optionItems.find(m => m.value === value) || {}
                msg.value.push(label + ' = ' + v.label)
              }
              if (label && value && type === 'select' && optionItems) {
                const v = optionItems.find(m => m.value === value) || {}
                msg.value.push(label + ' = ' + v.label)
              }
              if (label && value && type === 'checkbox' && optionItems) {
                const v = optionItems.filter(m => value.includes(m.value)).map(p => p.label)
                msg.value.push(label + ' = ' + v.join(','))
              }
            })
          }

          return msg
        })
      }

      return result
    })

    return {
      func,
      refNodeWrap,
      isTried,
      flowPermission1,
      approverConfig1,
      copyerConfig1,
      conditionsConfig1,
      m_nodeConfig,
      delNode,
      addTerm,
      delTerm,
      setPerson,
      arrTransfer,
      conditionMsg
    }
  }
}
</script>

<style lang="scss" scoped>

.node-wrap {
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
    padding: 0 50px;
    position: relative;
}

.node-wrap-box {
  .title .m-icon {
    font-size: 14px;
  }
}

.node-wrap-box .title {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    height: 24px;
    line-height: 24px;
    font-size: 12px;
    color: #fff;
    text-align: left;
    background: #576a95;
    border-radius: 4px 4px 0 0;
}

.node-wrap-box {
    display: -webkit-inline-box;
    display: -ms-inline-flexbox;
    display: inline-flex;
    flex-direction: column;
    position: relative;
    width: 220px;
    min-height: 72px;
    flex-shrink: 0;
    background: #fff;
    border-radius: 4px;
    cursor: pointer;
}

.node-wrap-box:after {
    pointer-events: none;
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: 4px;
    border: 1px solid transparent;
    transition: all .1s cubic-bezier(.645, .045, .355, 1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
}

.node-wrap-box.active:after,
.node-wrap-box:active:after,
.node-wrap-box:hover:after {
    border: 1px solid var(--ep-color-primary);
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, .3);
}

.node-wrap-box.active .close,
.node-wrap-box:active .close,
.node-wrap-box:hover .close {
    display: inherit;
}

.node-wrap-box.error:after {
    border: 1px solid #f25643;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
}

.node-wrap-box .placeholder {
    color: #bfbfbf;
}

.node-wrap-box .close {
    display: none;
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    font-size: 14px;
    color: #fff;
    border-radius: 50%;
    text-align: center;
    line-height: 20px;
}

.node-wrap-box .content {
    position: relative;
    font-size: 14px;
    padding: 16px;
    padding-right: 30px;
}

.node-wrap-box .content .text {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
}

.node-wrap-box .content .arrow {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 14px;
    font-size: 16px;
    color: var(--ep-color-info);
}

.start-node.node-wrap-box .content .text {
    display: block;
    white-space: nowrap;
}

.node-wrap-box:before {
    content: "";
    position: absolute;
    top: -12px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 4px;
    border-style: solid;
    border-width: 8px 6px 4px;
    border-color: var(--ep-color-info-light-5) transparent transparent;
    background: var(--ep-color-info-light-9);
}

.node-wrap-box.start-node:before {
    content: none;
}

.condition-node {
    min-height: 220px;
}

.condition-node,
.condition-node-box {
    display: inline-flex;
    flex-direction: column;
}

.condition-node-box {
    padding-top: 30px;
    padding-right: 50px;
    padding-left: 50px;
    justify-content: center;
    align-items: center;
    flex-grow: 1;
    position: relative;
}

.condition-node-box:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: var(--ep-color-info-light-5);
}

.add-branch {
    border: none;
    outline: none;
    user-select: none;
    justify-content: center;
    font-size: 12px;
    padding: 0 10px;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    color: var(--ep-color-primary);
    background: #fff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .1);
    position: absolute;
    top: -16px;
    left: 50%;
    transform: translateX(-50%);
    transform-origin: center center;
    cursor: pointer;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    transition: all .3s cubic-bezier(.645, .045, .355, 1);
}

.add-branch:hover {
    transform: translateX(-50%) scale(1.1);
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, .1);
}

.add-branch:active {
    transform: translateX(-50%);
    box-shadow: none;
}

.branch-wrap,
.node-wrap {
    display: inline-flex;
    width: 100%;
}

.branch-box-wrap {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    align-items: center;
    min-height: 270px;
    width: 100%;
    flex-shrink: 0;
}

.branch-box {
    display: flex;
    overflow: visible;
    min-height: 180px;
    height: auto;
    border-bottom: 2px solid #ccc;
    border-top: 2px solid #ccc;
    position: relative;
    margin-top: 15px;
}

.col-box {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    position: relative
}

.branch-box .col-box {
    background: var(--ep-color-info-light-9);
}

.branch-box .col-box:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: var(--ep-color-info-light-5);
}

.auto-judge {
    position: relative;
    width: 220px;
    min-height: 72px;
    background: #fff;
    border-radius: 4px;
    padding: 14px 19px;
    cursor: pointer;
}

.auto-judge:after {
    pointer-events: none;
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: 4px;
    border: 1px solid transparent;
    transition: all .1s cubic-bezier(.645, .045, .355, 1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
}

.auto-judge.active:after,
.auto-judge:active:after,
.auto-judge:hover:after {
    border: 1px solid var(--ep-color-primary);
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, .3);
}

.auto-judge.active .close,
.auto-judge:active .close,
.auto-judge:hover .close {
    display: block;
}

.auto-judge.error:after {
    border: 1px solid #f25643;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1);
}

.auto-judge .title-wrapper {
    position: relative;
    font-size: 12px;
    color: #15bc83;
    text-align: left;
    line-height: 16px;
}

.auto-judge .title-wrapper .editable-title {
    display: inline-block;
    max-width: 120px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.auto-judge .title-wrapper .priority-title {
    float: right;
    margin-right: 10px;
    color: var(--ep-color-info);
}

.auto-judge .placeholder {
    color: #bfbfbf;
}

.auto-judge .close {
    display: none;
    position: absolute;
    right: -10px;
    top: -10px;
    width: 20px;
    height: 20px;
    font-size: 14px;
    color: rgba(0, 0, 0, .25);
    border-radius: 50%;
    text-align: center;
    line-height: 20px;
    z-index: 2;
}

.flex {
  display: flex;
}

.auto-judge .content {
    font-size: 12px;

    .label {
      padding: 0 3px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
    }
}

.auto-judge .sort-left,
.auto-judge .sort-right {
    position: absolute;
    top: 0;
    bottom: 0;
    display: none;
    z-index: 1;
    color: var(--ep-color-info);
}

.auto-judge .sort-left {
    left: 0;
    border-right: 1px solid #f6f6f6;
}

.auto-judge .sort-right {
    right: 0;
    border-left: 1px solid #f6f6f6;
}

.auto-judge:hover .sort-left,
.auto-judge:hover .sort-right {
    display: flex;
    align-items: center;
}

.auto-judge .sort-left:hover,
.auto-judge .sort-right:hover {
    background: #efefef;
}

// .auto-judge:hover .editable-title,
// .node-wrap-box:hover .editable-title {
//     border-bottom: 1px dashed #fff;
// }

// .auto-judge:hover .editable-title.editing,
// .node-wrap-box:hover .editable-title.editing {
//     text-decoration: none;
//     border: 1px solid #d9d9d9;
// }

.auto-judge:hover .editable-title {
    border-color: #15bc83
}

.error_tip {
    position: absolute;
    top: 0px;
    right: 0px;
    transform: translate(150%, 0px);
    font-size: 24px;
}
.promoter_person .el-dialog__body {
    padding: 10px 20px 14px 20px;
}

.editable-title {
    line-height: 15px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    // border-bottom: 1px dashed transparent;
}

.editable-title:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 40px;
}

// .editable-title:hover {
//     border-bottom: 1px dashed #fff;
// }

// .editable-title-input {
//     flex: none;
//     height: 18px;
//     padding-left: 4px;
//     text-indent: 0;
//     font-size: 12px;
//     line-height: 18px;
//     z-index: 1;
// }

// .editable-title-input:hover {
//     text-decoration: none;
// }

.selected_list {
    margin-bottom: 20px;
    line-height: 30px;
}
.selected_list span {
    margin-right: 10px;
    padding: 3px 6px 3px 9px;
    line-height: 12px;
    white-space: nowrap;
    border-radius: 2px;
    border: 1px solid rgba(220, 220, 220, 1);
}
.selected_list img {
    margin-left: 5px;
    width: 7px;
    height: 7px;
    cursor: pointer;
}
.top-left-cover-line {
    left: -1px;
}

.top-left-cover-line,
.top-right-cover-line {
    position: absolute;
    height: 8px;
    width: 50%;
    background-color: var(--ep-color-info-light-9);
    top: -4px;
}

.top-right-cover-line {
    right: -1px;
}

.bottom-left-cover-line {
    left: -1px;
}

.bottom-left-cover-line,
.bottom-right-cover-line {
    position: absolute;
    height: 8px;
    width: 50%;
    background-color: var(--ep-color-info-light-9);
    bottom: -4px;
}

.bottom-right-cover-line {
    right: -1px;
}

</style>
